package ext;

import org.junit.Test;

public class IpToInt {

    public int ipToInt(String ip) {
        String[] ss = ip.split("\\.");
        int res = 0;
        for (int i = ss.length - 1; i >= 0; i--) {
            int cur = Integer.parseInt(ss[i]);
            cur <<= (ss.length - 1 - i) * 8;
            res |= cur;
        }
        return res;
    }

    public int ipToInt2(String ip) {
        String[] ss = ip.split("\\.");
        int res = 0;
        int t = 1;
        for (int i = ss.length - 1; i >= 0; i--) {
            int cur = Integer.parseInt(ss[i]);
            for (int j = 0; j < 8; j++) {
                if ((cur & 1) == 1) {
                    res += t;
                }
                cur >>>= 1;
                t <<= 1;
            }
        }
        return res;
    }

    @Test
    public void test() {
        System.out.println(ipToInt("10.0.3.193"));
        System.out.println(ipToInt2("10.0.3.193"));

    }
    
}
